home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / utils / easymenu.el.z / easymenu.el
Encoding:
Text File  |  1998-05-21  |  7.3 KB  |  219 lines

  1. ;;; easymenu.el - Easy menu support for Emacs 19 and XEmacs.
  2. ;; 
  3. ;; $Id: easymenu.el,v 5.9 1995/02/14 19:44:00 amanda Exp $
  4. ;;
  5. ;; LCD Archive Entry:
  6. ;; easymenu|Per Abrahamsen|abraham@iesd.auc.dk|
  7. ;; Easy menu support for XEmacs|
  8. ;; $Date: 1995/02/14 19:44:00 $|$Revision: 5.9 $|~/misc/easymenu.el.gz|
  9.  
  10. ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  11.  
  12. ;; This file is part of XEmacs.
  13.  
  14. ;; XEmacs is free software; you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation; either version 2, or (at your option)
  17. ;; any later version.
  18.  
  19. ;; XEmacs is distributed in the hope that it will be useful,
  20. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. ;; GNU General Public License for more details.
  23.  
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with XEmacs; if not, write to the Free Software
  26. ;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  27. ;; 02111-1307, USA.
  28.  
  29. ;;; Synched up with: Not synched with FSF.
  30.  
  31. ;; Commentary:
  32. ;;
  33. ;; Easymenu allows you to define menus for both Emacs 19 and XEmacs.
  34. ;;
  35. ;; This file 
  36. ;; The advantages of using easymenu are:
  37. ;;
  38. ;; - Easier to use than either the Emacs 19 and XEmacs menu syntax.
  39. ;;
  40. ;; - Common interface for Emacs 18, Emacs 19, and XEmacs.  
  41. ;;   (The code does nothing when run under Emacs 18).
  42. ;;
  43. ;; The public functions are:
  44. ;; 
  45. ;; - Function: easy-menu-define SYMBOL MAPS DOC MENU
  46. ;;     SYMBOL is both the name of the variable that holds the menu and
  47. ;;            the name of a function that will present a the menu.
  48. ;;     MAPS is a list of keymaps where the menu should appear in the menubar.
  49. ;;     DOC is the documentation string for the variable.
  50. ;;     MENU is an XEmacs style menu description.  
  51. ;;
  52. ;;     See the documentation for easy-menu-define for details.
  53. ;;
  54. ;; - Function: easy-menu-change PATH NAME ITEMS
  55. ;;     Change an existing menu.
  56. ;;     The menu must already exist and be visible on the menu bar.
  57. ;;     PATH is a list of strings used for locating the menu on the menu bar. 
  58. ;;     NAME is the name of the menu.  
  59. ;;     ITEMS is a list of menu items, as defined in `easy-menu-define'.
  60. ;;
  61. ;; - Function: easy-menu-add MENU [ MAP ]
  62. ;;     Add MENU to the current menubar in MAP.
  63. ;;
  64. ;; - Function: easy-menu-remove MENU
  65. ;;     Remove MENU from the current menubar.
  66. ;;
  67. ;; Emacs 19 never uses `easy-menu-add' or `easy-menu-remove', menus
  68. ;; automatically appear and disappear when the keymaps specified by
  69. ;; the MAPS argument to `easy-menu-define' are activated.
  70. ;;
  71. ;; XEmacs will bind the map to button3 in each MAPS, but you must
  72. ;; explicitly call `easy-menu-add' and `easy-menu-remove' to add and
  73. ;; remove menus from the menu bar.
  74.  
  75. ;;; Code:
  76.  
  77. ;; ;;;###autoload
  78. (defmacro easy-menu-define (symbol maps doc menu)
  79.   "Define a menu bar submenu in maps MAPS, according to MENU.
  80. The arguments SYMBOL and DOC are ignored; they are present for
  81. compatibility only.  SYMBOL is not evaluated.  In other Emacs versions
  82. these arguments may be used as a variable to hold the menu data, and a
  83. doc string for that variable.
  84.  
  85. The first element of MENU must be a string.  It is the menu bar item name.
  86. The rest of the elements are menu items.
  87.  
  88. A menu item is usually a vector of three elements:  [NAME CALLBACK ENABLE]
  89.  
  90. NAME is a string--the menu item name.
  91.  
  92. CALLBACK is a command to run when the item is chosen,
  93. or a list to evaluate when the item is chosen.
  94.  
  95. ENABLE is an expression; the item is enabled for selection
  96. whenever this expression's value is non-nil.
  97.  
  98. Alternatively, a menu item may have the form: 
  99.  
  100.    [ NAME CALLBACK [ KEYWORD ARG ] ... ]
  101.  
  102. Where KEYWORD is one of the symbol defined below.
  103.  
  104.    :keys KEYS
  105.  
  106. KEYS is a string; a complex keyboard equivalent to this menu item.
  107.  
  108.    :active ENABLE
  109.  
  110. ENABLE is an expression; the item is enabled for selection
  111. whenever this expression's value is non-nil.
  112.  
  113.    :suffix NAME
  114.  
  115. NAME is a string; the name of an argument to CALLBACK.
  116.  
  117.    :style STYLE
  118.    
  119. STYLE is a symbol describing the type of menu item.  The following are
  120. defined:  
  121.  
  122. toggle: A checkbox.  
  123.         Currently just prepend the name with the string \"Toggle \".
  124. radio: A radio button. 
  125. nil: An ordinary menu item.
  126.  
  127.    :selected SELECTED
  128.  
  129. SELECTED is an expression; the checkbox or radio button is selected
  130. whenever this expression's value is non-nil.
  131. Currently just disable radio buttons, no effect on checkboxes.
  132.  
  133. A menu item can be a string.  Then that string appears in the menu as
  134. unselectable text.  A string consisting solely of hyphens is displayed
  135. as a solid horizontal line.
  136.  
  137. A menu item can be a list.  It is treated as a submenu.
  138. The first element should be the submenu name.  That's used as the
  139. menu item in the top-level menu.  The cdr of the submenu list
  140. is a list of menu items, as above."
  141.   (` (progn
  142.        (defvar (, symbol) nil (, doc))
  143.        (easy-menu-do-define (quote (, symbol)) (, maps) (, doc) (, menu)))))
  144.  
  145. (defun easy-menu-do-define (symbol maps doc menu)
  146.   (if (featurep 'menubar)
  147.       (progn
  148.     (set symbol menu)
  149.     (fset symbol (list 'lambda '(e)
  150.                doc
  151.                '(interactive "@e")
  152.                '(run-hooks 'activate-menubar-hook)
  153.                '(setq zmacs-region-stays 't)
  154.                (list 'popup-menu symbol))))))
  155.  
  156. (defun easy-menu-change (&rest args)
  157.   (when (featurep 'menubar)
  158.     (apply 'add-menu args)))
  159.  
  160. ;; This variable hold the easy-menu mode menus of all major and
  161. ;; minor modes currently in effect in the current buffer.
  162. (defvar easy-menu-all-popups nil)
  163. (make-variable-buffer-local 'easy-menu-all-popups)
  164.  
  165. (defun easy-menu-add (menu &optional map)
  166.   "Add MENU to the current menu bar."
  167.   (if (featurep 'menubar)
  168.       (progn
  169.     (unless (member menu easy-menu-all-popups)
  170.       (push menu easy-menu-all-popups))
  171.     (setq mode-popup-menu (if (> (length easy-menu-all-popups) 1)
  172.                   (cons (easy-menu-title)
  173.                     (reverse easy-menu-all-popups))
  174.                 (car easy-menu-all-popups)))
  175.  
  176.     (cond ((null current-menubar)
  177.            ;; Don't add it to a non-existing menubar.
  178.            nil)
  179.           ((assoc (car menu) current-menubar)
  180.            ;; Already present.
  181.            nil)
  182.           ((equal current-menubar '(nil))
  183.            ;; Set at left if only contains right marker.
  184.            (set-buffer-menubar (list menu nil)))
  185.           (t
  186.            ;; Add at right.
  187.            (set-buffer-menubar (copy-sequence current-menubar))
  188.            (add-menu nil (car menu) (cdr menu)))))))
  189.  
  190. (defun easy-menu-remove (menu)
  191.   "Remove MENU from the current menu bar."
  192.   (if (featurep 'menubar)
  193.       (progn
  194.     (setq easy-menu-all-popups (delq menu easy-menu-all-popups)
  195.           mode-popup-menu (if (< (length easy-menu-all-popups) 1)
  196.                   (cons (easy-menu-title)
  197.                     (reverse easy-menu-all-popups))
  198.                 (car easy-menu-all-popups)))
  199.  
  200.     (and current-menubar
  201.          (assoc (car menu) current-menubar)
  202.          (delete-menu-item (list (car menu)))))))
  203.  
  204. ;; Think up a good title for the menu.  Take the major-mode of the
  205. ;; buffer, strip the -mode part, convert hyphens to spaces, and
  206. ;; capitalize it.
  207. ;;
  208. ;; If you can think of something smarter, feel free to replace it.
  209. ;; Don't forget to mail the change to xemacs@xemacs.org where everyone
  210. ;; can flame, er, praise your changes.
  211. (defun easy-menu-title ()
  212.   (capitalize (replace-in-string (replace-in-string
  213.                   (symbol-name major-mode) "-mode$" "")
  214.                  "-" " ")))
  215.  
  216. (provide 'easymenu)
  217.  
  218. ;;; easymenu.el ends here
  219.